package cn.hl.system.service.impl;

import cn.hl.basic.service.impl.BaseServiceImpl;
import cn.hl.system.domain.Role;
import cn.hl.system.mapper.RoleMapper;
import cn.hl.system.service.IRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * 业务实现类：
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {

    @Autowired
    private RoleMapper roleMapper;

    @Override
    @Transactional
    public void delete(Long id) {
        // 删除中间表t_role_permission
        roleMapper.deleteRolePermissionByRoleId(id);
        // 删除中间表t_role_menu
        roleMapper.deleteRoleMenuByRoleId(id);
        // 删除中间表t_employee_role
        roleMapper.deleteEmployeeRoleByRoleId(id);
        // 删除角色
        roleMapper.delete(id);
    }

    @Override
    @Transactional
    public void add(Role role) {
        // 添加角色
        roleMapper.add(role);
        // 添加中间表t_role_permission
        roleMapper.addRolePermission(role);
        // 添加中间表t_role_menu
        roleMapper.addRoleMenu(role);
    }

    @Override
    @Transactional
    public void update(Role role) {
        roleMapper.update(role);
        // 删除中间表t_role_permission
        roleMapper.deleteRolePermissionByRoleId(role.getId());
        // 删除中间表t_role_menu
        roleMapper.deleteRoleMenuByRoleId(role.getId());
        // 添加中间表t_role_permission
        roleMapper.addRolePermission(role);
        // 添加中间表t_role_menu
        roleMapper.addRoleMenu(role);
    }

    @Override
    public void patchDelete(List<Long> ids) {
        for (Long id : ids) {
            delete(id);
        }
    }
}
